import React, { useState } from 'react';
import { Switch, Radio } from 'antd';
import Editor from 'src/pages/AnalyseManage/EditReport/components/EditorStyleType/Editor';
import FormItem from 'antd/lib/form/FormItem';
import { filterObjectNullKeys } from 'src/libs/utils';

interface IProps {
	config: any;
	onValuesChange: ( chartType: string, key: string, values: Object, configType: string ) => void;
	form: any;
}

const PieLegend = ( props: IProps ) => {
	const { config, onValuesChange, form } = props;

	const [ isShowLegend, setShowLegend ] = useState( ( config.style && config.style.hasOwnProperty( 'isShowLegend' ) ) ? config.style.isShowLegend : false );
	const [ legendPosition, setPosition ] = useState( 'bottom' );
	const fontConfig = [
		'fontFamily', 'fontSize', 'fontColor', 'fontBold', 'fontItalic'
	];

	// console.log( 'leconfig', config );

	const onChange = ( checked: boolean ) => {
		setShowLegend( checked );
		form.setFieldsValue( { isShowLegend: checked } );
		onValuesChange( config.type, config.key, filterObjectNullKeys( form.getFieldsValue() ), 'style' );
	};

	const handlePositionChange = ( event ) => {
		setPosition( event.target.value );
		form.setFieldsValue( { legendPosition: event.target.value } );
		onValuesChange( config.type, config.key, filterObjectNullKeys( form.getFieldsValue() ), 'style' );
	};
	return (
		<div>
			<FormItem label="是否显示" name="isShowLegend" labelAlign="left" labelCol={{ span: 8 }} wrapperCol={{ span: 16 }}>
				<Switch checked={ isShowLegend } onChange={ onChange }/>
			</FormItem>

			{
				isShowLegend && (

					<div>
						<FormItem label="显示位置" name="legendPosition" labelAlign="left" labelCol={{ span: 8 }} wrapperCol={{ span: 16 }}>
							<Radio.Group value={ legendPosition } defaultValue={ legendPosition } onChange={ handlePositionChange }>
								<Radio.Button value="left">左边</Radio.Button>
								<Radio.Button value="right">右边</Radio.Button>
								<Radio.Button value="top">顶部</Radio.Button>
								<Radio.Button value="bottom">底部</Radio.Button>
							</Radio.Group>
						</FormItem>

						<FormItem label="图例字体" name="legendFont" labelAlign="left" labelCol={{ span: 8 }} wrapperCol={{ span: 16 }}>
							<Editor
								config={ config }
								form={ form }
								onValuesChange={ onValuesChange }
								fontFamilyName="legendFontFamily"
								fontSizeName="legendFontSize"
								fontColorName="legendFontColor"
								boldName='legendFontBold'
								italicName='legendFontItalic'
								fontConfig={ fontConfig }
							/>
						</FormItem>
					</div>
				)
			}
		</div>
	);
};

export default React.memo( PieLegend );
